package com.time.timespace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.time.timespace.commons.constant.BaseConstants;
import com.time.timespace.domain.entity.Comments;
import com.time.timespace.mapper.CommentsMapper;
import com.time.timespace.service.CommentsService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {

    private final CommentsMapper commentsMapper;

    public CommentsServiceImpl(CommentsMapper commentsMapper) {
        this.commentsMapper = commentsMapper;
    }

    @Override
    public List<Comments> selectAll() {
        return commentsMapper.selectAll();
    }

    @Override
    public Comments saveComments(Comments comments) {
        if (Objects.isNull(comments.getId())) {
            comments.setCreationDate(new Date());
            commentsMapper.insert(comments);
        } else {
            commentsMapper.updateById(comments);
        }
        Long id = comments.getId();
        return commentsMapper.selectById(id);
    }

    @Override
    public void deleteById(Long id) {
        Comments comments = commentsMapper.selectById(id);
        if (Objects.nonNull(comments)) {
            comments.setDeleteFlag(BaseConstants.Flag.Y);
            commentsMapper.updateById(comments);
        }
    }
}
